﻿<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>

<xsl:output indent="no" encoding="UTF-8" method="text" media-type="text/plain"/>

<xsl:key name="ProductCategoryName" match="row" use="c7" />
<xsl:variable name="artCount" select="4"/>
<xsl:variable name="rows" select="csv/row[(position()>1)]"/>
<xsl:variable name="imgSubPath">
		<xsl:call-template name="getMaxSubPath">
			<xsl:with-param name="paths" select="$rows/c4"/>
			<xsl:with-param name="pos" select="string-length($rows[1]/c4)"/>
		</xsl:call-template>
</xsl:variable>
<xsl:variable name="hrefSubPath">
		<xsl:call-template name="getMaxSubPath">
			<xsl:with-param name="paths" select="$rows/c5"/>
			<xsl:with-param name="pos" select="string-length($rows[1]/c5)"/>
		</xsl:call-template>
</xsl:variable>

<xsl:template match="/"  xml:space="strip">
	<xsl:variable name="kategorien" select="$rows[count(. | key('ProductCategoryName', c7)[1]) = 1]"/>
	
	<xsl:variable name="q1" >"</xsl:variable><xsl:variable name="q2">'</xsl:variable>
	<xsl:for-each select="$kategorien">
		<xsl:sort select="c7" />
		<xsl:variable name="categ" select="c7"/>
		<xsl:variable name="categPos" select="position()-1"/>
		<!-- selectiere alle artikel fuer aktuelle kategorie -->
		<xsl:variable name="rowsForCateg" select="$rows[(c7=$categ)]"/>
		<xsl:result-document href="{$categPos}.html" encoding="UTF-8" cdata-section-elements="script" omit-xml-declaration="yes" method="xhtml" media-type="text/html" indent="yes"
								doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" 
								doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

			<html>
			<head>
				<title><xsl:value-of select="$categ"/></title>
				<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
				<meta name="Author" content="Eugen Kremer"/>
				<link rel="stylesheet" href="../css/style.css" type="text/css"></link>
			</head>
			<body>
				<div class="orsay">
					<div id="select">
						<select id="selId" onchange="document.location=this.value+'.html'+document.location.search"/>
						<span id="nextId" onclick="var e = this.onclick.arguments[0]; e=e||event; var srcel = e.target||e.srcElement; if (!isNaN(srcel.innerHTML)) renderContent((new Number(srcel.innerHTML)-1)); return false;"/>
					</div>
					<div id="content"/>
				</div>
				<script type="text/javascript">
				<xsl:comment>
					try{
						var search_ = document.location.search.replace(/^\?/,'').split('&amp;');
						var res = new Array();
						for(var i=0; i&lt;search_.length; i++){
							if (/^libs/.test(search_[i]))
								res = search_[i].split(',');
						}
						for(var i=0; i&lt;res.length; i++)
							document.write('&lt;'+'script type=&quot;text/javascript&quot; src=&quot;http://www.google.com/ig/f/' + res[i]+'&quot;&gt;&lt;'+'/script&gt;');

					window.onload = function(){
							//alert('load');
							_IG_AdjustIFrameHeight();
						}
					}catch(e){}
				</xsl:comment>	
				</script>
				
				<script type="text/javascript">
				<xsl:comment>			
				<xsl:text>
					var data = {kategorien:[</xsl:text>
					<xsl:for-each select="$kategorien"><xsl:sort select="c7" />
						<xsl:text>"</xsl:text>
							<xsl:value-of select="translate(c7,$q1,$q2)"/>
						<xsl:text>"</xsl:text>
						<xsl:if test="position()!=last()"><xsl:text>,</xsl:text></xsl:if>
					</xsl:for-each>
				<xsl:text>], spImg:"</xsl:text>
				<xsl:value-of select="$imgSubPath"/>
				<xsl:text>", spHref:"</xsl:text>
				<xsl:value-of select="$hrefSubPath"/>
				<xsl:text>", artikel:[</xsl:text>
				<xsl:for-each select="$rowsForCateg">
					<xsl:text>{</xsl:text>
						<xsl:text>h:"</xsl:text><xsl:value-of select="substring-after(c5,$hrefSubPath)"/><xsl:text>",</xsl:text>
						<xsl:text>i:"</xsl:text><xsl:value-of select="substring-after(c4,$imgSubPath)"/><xsl:text>",</xsl:text>
						<xsl:text>a:"</xsl:text><xsl:value-of select="c2"/><xsl:text>",</xsl:text>
						<xsl:text>p:"</xsl:text><xsl:value-of select="c3"/><xsl:text>"</xsl:text>
					<xsl:text>}</xsl:text>
					<xsl:if test="position()!=last()"><xsl:text>,</xsl:text></xsl:if>
				</xsl:for-each>
				<xsl:text>]}
				</xsl:text>
				function _gel(id){return document.getElementById(id);}
				function _cel(tag){return document.createElement(tag);}
				var selPage = null;
				function renderContent(start){
					content.innerHTML = '';
					for(var i=start*count, c=(start+1)*count, c=(c&lt;data.artikel.length)?c:data.artikel.length; i&lt;c;i++){
						var d = _cel('div');
						var a = data.artikel[i];
						d.className = 'item'; d.innerHTML = "&lt;div&gt;&lt;a target='_blank' href='&quot;+data.spHref+a.h+&quot;' title='&quot;+a.a+&quot;'&gt;&lt;img src='&quot;+data.spImg+a.i+&quot;' alt='&quot;+a.a+&quot;'/&gt;&lt;/a&gt;&lt;b&gt;&quot;+a.p+&quot;&lt;/b&gt;&lt;/div&gt;";
						content.appendChild(d);
					}
					if (selPage!=null)
						selPage.className = '';
					selPage = _gel('nIdEl'+start);
					selPage.className = 'selPage';
				}
				var sel = _gel('selId'); var selected = document.location.href.match(/\/([^\/]*?)\.html/)[1];
				var count = document.location.search.match(/count=(\d+)/); count = (count!=null)?new Number(count[1]):4;
				for(var i=0,c=data.kategorien.length; i&lt;c;i++){
					var o = _cel('option'); 
					if (i==selected)
					o.setAttribute('selected', 'true');
					o.setAttribute('value', i);
					o.innerHTML = data.kategorien[i];
					sel.appendChild(o);
				}
				var content = _gel('content');
				var next = _gel('nextId');
				for(var i=0,l=data.artikel.length,m=l%count,c=(((l-m) / count)+((m>0)?1:0)); i&lt;c;i++){
					next.appendChild(document.createTextNode(' '));
					var d = _cel('a'); d.id='nIdEl'+i; d.href='#'; d.innerHTML = i+1;
					next.appendChild(d);
				}
				renderContent(0);
 				</xsl:comment>	
				</script>
			</body>
			</html>
		</xsl:result-document>
	</xsl:for-each>
</xsl:template>

<xsl:template name="getMaxSubPath">
	<xsl:param name="paths"/>
	<xsl:param name="pos"/>
	<xsl:variable name="sp" select="substring($paths[1], 1,$pos)"/>
	<xsl:variable name="cpaths" select="count($paths)"/>
	<xsl:variable name="cspaths" select="count($paths[contains(.,$sp)])"/>
	<xsl:choose>
		<xsl:when test="$cpaths=$cspaths">
			<xsl:value-of select="$sp"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="getMaxSubPath">
				<xsl:with-param name="paths" select="$paths"/>
				<xsl:with-param name="pos" select="number($pos - 1)"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="row">
	<xsl:text>{</xsl:text>
		<xsl:text>h:"</xsl:text><xsl:value-of select="substring-after(c5,$hrefSubPath)"/><xsl:text>",</xsl:text>
		<xsl:text>i:"</xsl:text><xsl:value-of select="substring-after(c4,$imgSubPath)"/><xsl:text>",</xsl:text>
		<xsl:text>a:"</xsl:text><xsl:value-of select="c2"/><xsl:text>",</xsl:text>
		<xsl:text>p:"</xsl:text><xsl:value-of select="c3"/><xsl:text>"</xsl:text>
	<xsl:text>}</xsl:text>
</xsl:template>

</xsl:stylesheet>